#include <unistd.h>
#include <string>
#include "Mutex.hpp"

int tickes = 1000;
Mutex t;

void *routine(void *args)
{
    std::string name = static_cast<const char *>(args);
    while (true)
    {
        // 所有的线程都需要申请锁
        {
            LockGuard l;
            // t.Lock();
            if (tickes > 0)
            {
                usleep(1000);
                printf("%s : %d\n", name.c_str(), tickes--);
                // t.Unlock();
            }
            else
            {
                // t.Unlock();
                break;
            }
        }
    }

    return (void *)0;
}

int main()
{
    pthread_t t1, t2, t3, t4;

    pthread_create(&t1, nullptr, routine, (void *)"thread-1");
    pthread_create(&t2, nullptr, routine, (void *)"thread-2");
    pthread_create(&t3, nullptr, routine, (void *)"thread-3");
    pthread_create(&t4, nullptr, routine, (void *)"thread-4");

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}